'use strict';
import * as Koa from 'koa';
import { router, prefix } from '../middleware/router';
import { platform_userService as userService } from '../service/platform_user';
import { response } from '../middleware/result';
import { Exceptions } from '../kalend';
import { customer_accountService } from '../service/customer_account';
import { ObjectId } from 'bson';
/** 后台用户API对象 */
@prefix('puser')
class Platform_userController {
    /**
     * pc后台登录
     * @api {POST} /puser/login PC端用户登录
     * @apiDescription 根据用户手机号和密码登录
     * @apiName login
     * @apiParam (参数) {String} phone
     * @apiParam (参数) {String} password
     * @apiSampleRequest /puser/login
     * @apiGroup puser
     * @apiVersion 1.0.0
     */
    @router('/login', { method: 'post' })
    async login(ctx: Koa.Context) {
        let result = await userService.login(ctx.request.body.phone, ctx.request.body.password);
        response(ctx, result);
    }
    @router('/create', { method: 'post' })
    async create(ctx: Koa.Context) {
        let doc = ctx.request.body;
        if (doc.relatephone) {
            let customer = await customer_accountService.querySingle({ phone: doc.relatephone });
            if (!customer) {
                throw Exceptions.known('该手机号会员不存在');
            }
            if (await userService.exist({ phone: doc.relatephone })) {
                throw Exceptions.known('已存在关联该会员的用户，添加失败');
            }
            doc.customerid = customer._id;
        }
        if (await userService.exist({ phone: doc.phone })) {
            throw Exceptions.known('已存在此手机号的用户，添加失败');
        }
        if (doc.phone && doc.phone.length !== 11) {
            throw Exceptions.known('请输入正确11位手机号');
        }
        let result = await userService.create(doc);
        response(ctx, result);
    }
    @router('/update', { method: 'post' })
    async update(ctx: Koa.Context) {
        let doc = ctx.request.body;
        if (doc.relatephone) {
            let customer = await customer_accountService.querySingle({ phone: doc.relatephone });
            if (!customer) {
                throw Exceptions.known('该手机号会员不存在，设置失败');
            } else {
                if (customer.inviter) {
                    throw Exceptions.known('该小程序账号被邀请过，设置失败');
                }
            }
            if (await userService.exist({ phone: doc.relatephone, _id: { $ne: doc._id } })) {
                throw Exceptions.known('已存在关联该会员的用户，设置失败');
            }
            doc.customerid = customer._id;
        }
        if (doc.phone) {
            if (await userService.exist({ phone: doc.phone, _id: { $ne: doc._id } })) {
                throw Exceptions.known('已存在此手机号的用户，修改失败');
            }
        }
        let result = await userService.update(doc._id, doc);
        response(ctx, result);
    }
    /**
    * 禁用用户
    * @api {get} /puser/lock 禁用用户
    * @apiDescription 禁用用户
    * @apiName lock
    * @apiParam (参数) {String} userid  用户id
    * @apiSampleRequest /puser/lock
    * @apiGroup puser
    * @apiVersion 1.0.0
    */
    @router('/lock/:userid')
    async lock(ctx: Koa.Context) {
        let result = await userService.lock(ctx.params.userid);
        response(ctx, result);
    }
    /**
    * 启用用户
    * @api {get} /puser/open 启用用户
    * @apiDescription 启用用户
    * @apiName open
    * @apiParam (参数) {String} userid  用户id
    * @apiSampleRequest /puser/open
    * @apiGroup puser
    * @apiVersion 1.0.0
    */
    @router('/open/:userid')
    async open(ctx: Koa.Context) {
        let result = await userService.open(ctx.params.userid);
        response(ctx, result);
    }
    /**
    * 获取后台用户信息
    * @api {get} /puser/queryById 获取后台用户信息
    * @apiDescription 获取后台用户信息
    * @apiName queryById
    * @apiParam (参数) {String} id  用户id
    * @apiSampleRequest /puser/queryById
    * @apiGroup puser
    * @apiVersion 1.0.0
    */
    @router('/queryById/:id')
    async queryById(ctx: Koa.Context) {
        let result = await userService.queryById(ctx.params.id);
        response(ctx, result);
    }
    /**
     * 
     * @api {post} /puser/query  查询用户
     * @apiDescription 查询用户
     * @apiName query
     * @apiParam (参数) {String} offset   页码
     * @apiParam (参数) {String} limit    每页显示条数
     * @apiParam (参数) {String} name    用户名/手机号
     * @apiParam (参数) {String} status  用户状态
     * @apiSampleRequest /puser/query
     * @apiGroup puser
     * @apiVersion 1.0.0
     */
    @router('/query', { method: 'post' })
    async query(ctx: Koa.Context) {
        let result = await userService.query(ctx.request.body);
        response(ctx, result);
    }

    @router('/getuser', { method: 'post' })
    async getuser(ctx: Koa.Context) {
        let result = await userService.getuser(ctx.request.body);
        response(ctx, result);
    }


    @router('/getcustomeruser', { method: 'post' })
    async getcustomeruser(ctx: Koa.Context) {
        let result = await customer_accountService.querySingle({ _id: ctx.request.body.customerid });
        response(ctx, result);
    }

    @router('/getSingleUser', { method: 'post' })
    async getSingleUser(ctx: Koa.Context) {
        let result = await userService.getSingleUser(ctx.request.body);
        response(ctx, result);
    }
    @router('/updatepwd', { method: 'post' })
    async updatepwd(ctx: Koa.Context) {
        let result = await userService.updatepwd(ctx.request.body);
        response(ctx, result);
    }

    @router('/editPass', { method: 'post' })
    async editPass(ctx: Koa.Context) {
        let res: any = await userService.editPass(ctx.request.body);
        response(ctx, res);
    }
}